/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：给脚本添加自定义的头部注释
* CreateTime：2020-04-05 19:06:34
* Version：1.0
* Modify Recorder：
*************************************************/
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class AutoAddScriptComment : UnityEditor.AssetModificationProcessor
    {
        private static string CommentContent =
                         "/*************************************************\r\n"
                       + "* Title：#TITLE\r\n"
                       + "* Author：#AUTHOR \r\n"
                       + "* Describe：\r\n"
                       + "* CreateTime：2020-04-05 19:06:34\r\n"
                       + "* Version：1.0\r\n"
                       + "* Modify Recorder：\r\n"
                       + "*************************************************/\r\n";

        /// <summary>  
        /// 在创建资源时调用
        /// </summary>
        public static void OnWillCreateAsset(string path)
        {
            AddComment(path); 
        }

        private static string AddComment(string path)
        {
            path = path.Replace(".meta", "");
            if (!path.EndsWith(".cs")) return null;
            string tempStr = CommentContent;
            string content = File.ReadAllText(path);
            if (content.StartsWith("/*******************")) return "";
            tempStr += content;
            tempStr = tempStr.Replace("2020-04-05 19:06:34", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            
            TinyToolEditorSettingAsset editorSettingAsset = Resources.Load<TinyToolEditorSettingAsset>("TinyToolEditorSetting");
            if (editorSettingAsset != null)
            {
                tempStr = tempStr.Replace("#TITLE",editorSettingAsset.appTitle)
                    .Replace("#AUTHOR",editorSettingAsset.author);
            }
            
            File.WriteAllText(path, tempStr);

            return Path.GetFileNameWithoutExtension(path);
        }

        public static bool AddHeaderCommentValidate()
        {
            //Selection.objects 返回场景或者Project中选择的资源
            if (Selection.objects.Length > 0)
            {
                for (int i = 0; i < Selection.objects.Length; i++)
                {
                    if (Selection.objects[i] != null && Selection.objects[i].GetType().Name.Equals("MonoScript"))
                        return true;
                }
            }
            return false;
        }

        public static void AddHeaderComment()    
        {
            List<string> names = new List<string>();
            foreach (Object item in Selection.objects)
            {
                string path = AssetDatabase.GetAssetPath(item);
                string scriptName = AddComment(path);
                if (string.IsNullOrEmpty(scriptName) == false)
                    names.Add(scriptName);
            }
            if (names.Count > 0)
            {
                string content = string.Join("、", names);
                EditorUtility.DisplayDialog("添加头部注释", content + " 成功添加头部注释", "确定");
            }
        }
    }
}

